using System;
using System.IO;
using System.Reflection;
using Microsoft.Win32;

namespace mylibrary
{
    public static class ApplicationAttributes
    {
        private const string RunLocation = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
        private static string AssemblyLocation = Assembly.GetExecutingAssembly().Location;

        #region Public variables and properties
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return the Title
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string AssemblyTitle
        {
            get
            {
                // Get all Title attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                // If there is at least one Title attribute
                if (attributes.Length > 0)
                {
                    // Select the first one
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    // If it is not an empty string, return it
                    if (titleAttribute.Title != "")
                        return titleAttribute.Title;
                }
                // If there was no Title attribute, or if the Title attribute was the empty string, return the .exe name
                return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return the Version
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return the Description
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string AssemblyDescription
        {
            get
            {
                // Get all Description attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                // If there aren't any Description attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Description attribute, return its value
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return the Productname
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string AssemblyProduct
        {
            get
            {
                // Get all Product attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                // If there aren't any Product attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Product attribute, return its value
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return the Copyright
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string AssemblyCopyright
        {
            get
            {
                // Get all Copyright attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                // If there aren't any Copyright attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Copyright attribute, return its value
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return the Company
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string AssemblyCompany
        {
            get
            {
                // Get all Company attributes on this assembly
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                // If there aren't any Company attributes, return an empty string
                if (attributes.Length == 0)
                    return "";
                // If there is a Company attribute, return its value
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return the Local Application Data Path
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string LocalApplicationDataPath
        {

            get
            {
                string TheLocalApplicationDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                string CompanyPath = Path.Combine(TheLocalApplicationDataPath, AssemblyCompany);
                string ApplicationDataPath = Path.Combine(CompanyPath, AssemblyTitle);
                if (Directory.Exists(CompanyPath) == false)
                {
                    Directory.CreateDirectory(CompanyPath);
                }
                if (Directory.Exists(ApplicationDataPath) == false)
                {
                    Directory.CreateDirectory(ApplicationDataPath);
                }

                return ApplicationDataPath;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return the Path of the current running program
        //---------------------------------------------------------------------------------------------------------------------------------------
        public static string ProgramPath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory;
            }
        }

        public static void EnableAutoStart(string keyName)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(RunLocation);
            key.SetValue(keyName, AssemblyLocation);
        }

        public static void DisableAutoStart(string keyName)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(RunLocation);
            key.DeleteValue(keyName);
        }

        public static bool IsAutoStartEnabled(string keyName)
        {
            bool TheResult = false;
            RegistryKey key = Registry.CurrentUser.OpenSubKey(RunLocation);
            if (key != null)
            {
                string value = (string)key.GetValue(keyName);
                if (value != null)
                {
                    TheResult = (value == AssemblyLocation);
                }
            }
            return TheResult;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion
    }

}
